package com.zidian.teacher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.zidian.teacher.R;
import com.zidian.teacher.bean.ListGroupBean;

import java.util.List;


/**
 * Created by Administrator on 2016/9/6 0006.
 */
public class ManageGroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int FOOT_TYPE = 0;
    private static final int ITEM_TYPE = 1;

    private Context context;
    private List<ListGroupBean> groupBeanList;


    public ManageGroupAdapter(Context context, List<ListGroupBean> groupBeanList) {
        this.context = context;
        this.groupBeanList = groupBeanList;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == groupBeanList.size()) {
            return FOOT_TYPE;
        } else {
            return ITEM_TYPE;
        }
    }

    public interface OnButtonClickListener {
        void onDeleteBtnClick(int position);

        void onEditBtnClick(int position);
    }

    private OnButtonClickListener onButtonClickListener;

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == FOOT_TYPE) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_group_manage_foot, parent,
                    false);
            return new FootViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_group_manage, parent,
                    false);
            return new ItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).total.setText(groupBeanList.get(position).getGroupTotal());
            ((ItemViewHolder) holder).groupName.setText(groupBeanList.get(position).getGroupName());
            if (position + 1 == groupBeanList.size()) {
                ((ItemViewHolder) holder).delete.setVisibility(View.GONE);
                ((ItemViewHolder) holder).editBtn.setVisibility(View.GONE);
            } else {
                ((ItemViewHolder) holder).delete.setVisibility(View.VISIBLE);
                ((ItemViewHolder) holder).editBtn.setVisibility(View.VISIBLE);
            }

            if (onButtonClickListener != null) {
                ((ItemViewHolder) holder).editBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int position = holder.getLayoutPosition();
                        onButtonClickListener.onEditBtnClick(position);


                    }
                });

                ((ItemViewHolder) holder).delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();

                        onButtonClickListener.onDeleteBtnClick(position);
                    }
                });
            }


            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });
            }


        } else if (holder instanceof FootViewHolder) {


            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });
            }


        }


    }

    @Override
    public int getItemCount() {
        return groupBeanList.size() == 0 ? 0 : groupBeanList.size() + 1;
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView total;
        TextView groupName;
        ImageView delete;
        Button editBtn;


        public ItemViewHolder(View view) {
            super(view);
            total = (TextView) view.findViewById(R.id.manage_group_total);
            groupName = (TextView) view.findViewById(R.id.manage_group_name);
            delete = (ImageView) view.findViewById(R.id.manage_group_delete);
            editBtn = (Button) view.findViewById(R.id.manage_group_edit_btn);
        }
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {

        public FootViewHolder(View view) {
            super(view);
        }
    }


}
